<?php
$params = $_SERVER['argv'];
$start_page = $params[1]??"";
if(empty($start_page)){
	var_dump("start_page错误");
	exit;
}
$file_count = $params[2]??"";
if(empty($file_count)){
	var_dump("file_count错误");
	exit;
}
$file_dir = $params[3]??"";
if(empty($file_dir)){
	var_dump("file_dir错误");
	exit;
}

$fp = "D:/LeanSpace/shengshi/$file_dir";
if (!file_exists($fp)) {
    mkdir($fp, 0777, true);
}

$host = "https://nnhm7.com";
$start_url = $host.$start_page;

$is_continue = true;
while ($is_continue) {
	$start_html = file_get_contents($start_url);
	$image_list_pattern = '/<img class="lazy" width=".*?" data-original="(.*?jpg)".*?>/';
	preg_match_all($image_list_pattern, $start_html, $matches);
	foreach ($matches[1] as $image_url) {
		file_put_contents("D:/LeanSpace/shengshi/$file_dir/$file_count.jpg", file_get_contents($image_url));
		var_dump($file_count."完成");
		$file_count++;
	}

	$next_page_pattern = '/<a href="(.*)" id="k_Pic_nextArr">下一章<\/a>/';
	preg_match($next_page_pattern, $start_html, $next);
	var_dump("开始寻找下一章");
	var_dump($next);
	if(!empty($next[1])){
		$start_url = $host.$next[1];
	}else{
		$is_continue = false;
	}
}
var_dump("最终完成");
